package hander

import (
	"chat/api/request"
	"chat/consts"
	"chat/internal/dao"
	"chat/internal/model"
	"chat/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Login(c *gin.Context) {
	var req request.LoginReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var user model.User
	dao.GetOneById(req.Id, &user)
	if user.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "用户不存在",
			"code": 500,
			"data": nil,
		})
		return
	}
	token, _ := pkg.NewJWT(consts.JWT_KEY).CreateToken(pkg.CustomClaims{
		ID: user.Id,
	})
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": token,
	})
}
